package org.jcb.view;

import org.jcb.ChinoisActivity;
import org.jcb.R;
import org.jcb.dao.data.ChineseDatabase;
import org.jcb.dao.data.GlobalDAO;
import org.jcb.model.ChineseQuestionnaire;
import org.jcb.model.DataChineseLanguage;
import org.jcb.model.EndOfQuestionException;
import org.jcb.model.User;

import android.content.Context;
import android.media.MediaPlayer;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

public class QuizzView extends LinearLayout {
	ChineseQuestionnaire questionnaire ;
	Button[] buttonChoice = new Button[4] ;
	TextView tViewScore ;
	ImageButton imageButton ;
	User user = new User("jcb");
	ChinoisActivity chinoisActivity ;
	//int idQuizz ;
	
	public QuizzView(Context context,ChineseQuestionnaire questionnaire) {
		super(context);
		this.questionnaire = questionnaire;
		this.chinoisActivity = (ChinoisActivity) context;
		LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		layoutInflater.inflate(R.layout.quizz_pinyin,this);
		buttonChoice[0]  = (Button) findViewById(R.id.buttonChoice1);
		buttonChoice[1]  = (Button) findViewById(R.id.buttonChoice2);
		buttonChoice[2]  = (Button) findViewById(R.id.buttonChoice3);
		buttonChoice[3] = (Button) findViewById(R.id.buttonChoice4);
		imageButton 	= (ImageButton) findViewById(R.id.imageButtonSound);
		tViewScore		= (TextView) findViewById(R.id.scoreQuizz);
		
			buttonChoice[0].setOnClickListener(new View.OnClickListener() {
		        public void onClick(View v) {
		        	analyseReponse((String) buttonChoice[0].getText());
		        	nextItem();
		        }
		    });	
		
			buttonChoice[1].setOnClickListener(new View.OnClickListener() {
		        public void onClick(View v) {
		        	analyseReponse((String) buttonChoice[1].getText());
		        	nextItem();
		        }
		    });	

			buttonChoice[2].setOnClickListener(new View.OnClickListener() {
		        public void onClick(View v) {
		        	analyseReponse((String) buttonChoice[2].getText());
		        	nextItem();
		        }
		    });	

			buttonChoice[3].setOnClickListener(new View.OnClickListener() {
		        public void onClick(View v) {
		        	analyseReponse((String) buttonChoice[3].getText());
		        	nextItem();
		        }
		    });	

			imageButton.setOnClickListener(new View.OnClickListener() {
		        public void onClick(View v) {
		        	repeatSon();		        	
		        }
		    });	

		
		firstItem();
	}

	public void repeatSon() {
		DataChineseLanguage dcl = questionnaire.getDataChinese() ;
		int sound = ChineseDatabase.getSound(questionnaire.getPinyin());
		if (sound > 0) {
			//ChinoisActivity.mediaPlayer = MediaPlayer.create(this.getContext(), sound);
			ChinoisActivity.mediaPlayer.start();
		}		
	}
	
	public void analyseReponse(String reponseUser) {
		DataChineseLanguage dcl = questionnaire.getDataChinese() ;
		//on enregistre
		ChinoisActivity.globalDAO.recordAnswerUser(questionnaire.getIdQuizz(), dcl.getPinyin(), reponseUser, user);
		
		if (dcl.getPinyin().equals(reponseUser)) {
			dcl.setCorrect(Boolean.TRUE);
		}
		String sScore = "Q" + questionnaire.getCurrent() + "  Score : " + questionnaire.getScore() + " / " + questionnaire.getHighScore();		
		tViewScore.setText(sScore);
		
	}
	
	public void firstItem() {
		try {
			questionnaire.start();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			Log.i("", e.toString());
		}
		display() ;
	}
	
	public void nextItem() {
		Log.i("", "aaaaaaaaaaa");
		try {
			questionnaire.nextQuestion();
			display() ;
		} catch (EndOfQuestionException e) {
			Log.i("", e.toString());
			chinoisActivity.endQuizzPinyin(questionnaire);
		}
		
	}	

	public void display() {
		DataChineseLanguage dcl = questionnaire.getDataChinese() ;
		
		for (int i = 0 ; i < buttonChoice.length ; i++) {
			if (dcl.getReponsesTxtPossibles().size() > i ) {
				buttonChoice[i].setText(dcl.getReponsesTxtPossibles().get(i));
			}
			else {
				buttonChoice[i].setText("?");
			}
		}
		
		
		int sound = ChineseDatabase.getSound(questionnaire.getPinyin());
		if (sound > 0) {
			ChinoisActivity.mediaPlayer = MediaPlayer.create(this.getContext(), sound);
			ChinoisActivity.mediaPlayer.start();
		}

	}
	
	
}
